package com.zidian.teacher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zidian.teacher.R;
import com.zidian.teacher.bean.QuestionDetailBean;
import com.zidian.teacher.bean.QuestionDetailListBean;
import com.zidian.teacher.utils.GetUserInfo;
import com.zidian.teacher.utils.ProgressBarBlackUtil;
import com.zidian.teacher.utils.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import com.zidian.teacher.adapter.QuestionDetailAdapter;
import com.zidian.teacher.common.Constant;
import com.zidian.teacher.common.ToolBarActivity;

public class QuestionDetailActivity extends ToolBarActivity {

    @Bind(R.id.test)
    TextView test;
    @Bind(R.id.question_detail_option)
    TextView questionDetailTitle;
    @Bind(R.id.question_detail_list)
    RecyclerView questionDetailList;
    @Bind(R.id.question_detail_total)
    TextView questionDetailTotal;

    private int questionnaireId;
    private String questionnaireTitle;
    private int studentsNumber;

    private QuestionDetailAdapter adapter;

    private List<QuestionDetailBean.DataBean> data = new ArrayList<>();
    private List<QuestionDetailListBean> list = new ArrayList<>();
    private static final String[] OPTION_NUMBER = {"A","B","C","D","E","F","G","H"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_question_detail);
        ButterKnife.bind(this);
        setTitle("问卷详情");
        Intent intent = getIntent();
        questionnaireId = intent.getIntExtra("questionnaireId",0);
        questionnaireTitle = intent.getStringExtra("questionnaireTitle");
        studentsNumber = intent.getIntExtra("studentsNumber",1);
        initView();
    }

    private void initView() {
        getQuestionDetail(questionnaireId);
        adapter = new QuestionDetailAdapter(this,list,studentsNumber);
        questionDetailList.setLayoutManager(new LinearLayoutManager(this));
        //初始化适配器
        questionDetailList.setAdapter(adapter);
    }

    /**
     * 获取问卷详情
     *
     */
    private void getQuestionDetail(int questionId) {
        String url = Constant.URL_TEACHER + "Questionnaire/selectQuestionnaireStatistics";
        Map<String, String> map = new HashMap<>();

        map.put("teacherId", GetUserInfo.getTeacherId(this));
        map.put("token", GetUserInfo.getToken(this));
        map.put("questionnaireId", questionId+"");
        ProgressBarBlackUtil.loadShow(this);
        OkHttpUtils.post().url(url).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ProgressBarBlackUtil.loadCancel();
                ToastUtil.showToast(QuestionDetailActivity.this, e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                ProgressBarBlackUtil.loadCancel();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(response);
                    int resultCode = jsonObject.getInt("code");
                    if (resultCode != 200) {
                        questionDetailTitle.setText(jsonObject.getString("message"));
                        return;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Gson gson = new Gson();
                QuestionDetailBean model = gson.fromJson(response,QuestionDetailBean.class);
                data = model.getData();

                //用于调试使用
//                test.setText(model.toString());
                questionDetailTitle.setText(questionnaireTitle);
                questionDetailTotal.setText("已填:"+studentsNumber+"人");
                setListData(data);
                adapter.setData(list);
            }
        });
    }

    /**
     * 设置RecyclerView的数据
     * @param bean
     */
    private void setListData(List<QuestionDetailBean.DataBean> bean){
        for (int i = 0; i < bean.size(); i++) {
            String title = "(" + bean.get(i).getQuestionNum() + ")" + bean.get(i).getQuestionContent();
            list.add(new QuestionDetailListBean(title,0,false ));
            List<QuestionDetailListBean> sortList = new ArrayList<>();
            for (int j = 0; j < bean.get(i).getStatistical().size(); j++) {
                QuestionDetailBean.DataBean.Statistical statistical = bean.get(i).getStatistical().get(j);
                String option = OPTION_NUMBER[Integer.parseInt(statistical.getOption())-1] + "."+statistical.getOptionsDescribe();

                QuestionDetailListBean listBean = new QuestionDetailListBean();
                listBean.setChild(true);
                listBean.setItemName(option);
                listBean.setOptionNumber(statistical.getStisNum());
                listBean.setOrder(Integer.parseInt(statistical.getOption()));
                sortList.add(listBean);
            }
            //对选项进行排序
            Collections.sort(sortList);
            list.addAll(sortList);
        }


    }
}
